home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / editor / auror300.zip / TEMPLATE.AML < prev    next >
Text File  |  1996-07-17  |  4KB  |  181 lines

  1. //--------------------------------------------------------------------
  2. // TEMPLATE.AML
  3. // Template Editing, (C) 1993-1996 by nuText Systems
  4. //
  5. // (see Template.dox and Template.dat for user help)
  6. //
  7. // This macro installs or removes Template Editing. This macro requires
  8. // the file Template.dat, which contains user-modifiable templates. See
  9. // Template.dat for further instructions.
  10. //
  11. // Usage:
  12. //
  13. // Select this macro from the Macro List (on the Macro menu), or run it
  14. // from the macro picklist <shift f12>
  15. //--------------------------------------------------------------------
  16.  
  17. include bootpath "define.aml"
  18.  
  19. // check for a previous install
  20. obj = prf.templobj
  21. if obj then
  22.   if (okbox "Template is already installed. Remove it?") == 'Ok' then
  23.     destroyobject obj
  24.     destroyvar "templobj" "prf"
  25.     msgbox "Template removed."
  26.   end
  27.   return
  28. else
  29.   prf.templobj = getcurrobj
  30. end
  31.  
  32. // keep this object resident
  33. resident ON
  34.  
  35. // template expansion only in edit windows
  36. object edit
  37.  
  38. // template data file name
  39. templatefile = getbootpath + "macro\\template.dat"
  40.  
  41. // template expansion key
  42. // (change this key definition to suit your own preferences)
  43.  
  44. key <ctrl f3>
  45.  
  46.   // get the template keyword and start column
  47.   pushcursor
  48.   if find _CSet "~lr" then
  49.     right
  50.   else
  51.     col 1
  52.   end
  53.   startcol = getcol
  54.   word = getword
  55.   popcursor
  56.  
  57.   // if no template keyword is found, then return
  58.   if not word then
  59.     display
  60.     say "No template keyword" 'b'
  61.     return
  62.   end
  63.  
  64.   // get the current buffer id
  65.   buffer = getcurrbuf
  66.  
  67.   // build the template key
  68.   tempkey = '^' + word + '{{\\.[a-zA-Z]*}*\\' + (locase (getext (getbufname))) +
  69.             "\\.| |$}|{\\. |$}"
  70.  
  71.   undobegin
  72.  
  73.   // load the template file
  74.   if loadbuf templatefile then
  75.  
  76.     // look for the template key
  77.     len = find tempkey 'xi*'
  78.     if len then
  79.  
  80.       // look for explicitly specified number of lines, if any
  81.       right len
  82.       len = find '[0-9]#' 'xl*'
  83.       if len then
  84.         lines = gettext (getcol) len
  85.  
  86.       // no explicit lines specified -- look for the next blank line
  87.       else
  88.         top = getrow
  89.         pushcursor
  90.         if find '^$' 'x' then
  91.           lines = getrow - top - 1
  92.         else
  93.           lines = getlines - top
  94.         end
  95.         popcursor
  96.       end
  97.  
  98.       // delete the original word in the text
  99.       delchar  length word  startcol '' buffer
  100.  
  101.       oldmark = usemark 'T'
  102.  
  103.       // insert the first template line horizontally
  104.       down
  105.       markcolumn 1 (getlinelen)
  106.       copyblock 'T' buffer startcol
  107.  
  108.       // insert the following lines vertically, and shift right if needed
  109.       if lines > 1 then
  110.         down
  111.         markline '' getrow + lines - 2
  112.         copyblock 'T' buffer
  113.         if startcol > 1 then
  114.           shiftblock startcol - 1
  115.         end
  116.       end
  117.  
  118.       // destroy the template buffer
  119.       destroybuf
  120.  
  121.       //gotobuf buffer
  122.       markline (getrow) getrow + lines - 1
  123.  
  124.       // expand embedded macro expressions, if any
  125.       pushcursor
  126.       loop
  127.         len = find "\\\\\\{.+\\}" "xb*"
  128.         if not len
  129.           break
  130.         end
  131.  
  132.         // get the macro expression
  133.         begcolumn = getcol
  134.         expression = gettext begcolumn + 2  len - 3
  135.  
  136.         // delete the expression in the copied text
  137.         delchar len
  138.  
  139.         // evaluate the expression
  140.         value = eval expression
  141.         // (expression may have changed the current buffer)
  142.         gotobuf buffer
  143.  
  144.         // insert the value of the expression into the text
  145.         // or indicate a compilation error
  146.         error = geterror 'c'
  147.         if error then
  148.           errorstr = "Template expression error: " + (errormsg error)
  149.           break
  150.         else
  151.           instext value
  152.           right  length value
  153.         end
  154.       end
  155.       popcursor
  156.  
  157.       // move to the cursor position specified in the template (if any)
  158.       replace "\\c" ' ' "gbi*"
  159.  
  160.       destroymark
  161.       usemark oldmark
  162.     else
  163.       errorstr = "Template '" + word + "' not found"
  164.       destroybuf
  165.     end
  166.   else
  167.     errorstr = "Can't open " + templatefile
  168.   end
  169.  
  170.   undoend
  171.  
  172.   // display any error messages
  173.   if errorstr then
  174.     display
  175.     say errorstr 'b'
  176.   end
  177. end
  178.  
  179. display
  180. say "Template installed. See Template.dat for instructions.":-80
  181.